package pl.olap.main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import pl.olap.dao.Genre;
import pl.olap.dao.Movie;
import pl.olap.dao.Person;
import pl.olap.dao.Premiere;
import pl.olap.dao.Production;
import pl.olap.persistence.SaveEntityUtil;
import pl.olap.sites.Filmweb;
import pl.olap.sites.Parser;

public class FilmwebRunner {

	private static int number = 2;

	public static void getDataFromFilmweb() {
		Thread thread = new Thread() {
			@Override
			public void run() {
				Filmweb site = new Filmweb();
				Connector connector = new Connector(site.URL_MAIN + "1");
				String content;
				try {
					content = connector.getContent();
					site.setCountPages(content);
					// getSubSite(site, content);
					for (int i = 1; i < site.getCountPages();i++) {
						connector.connect(site.getNextUrl());
						content = connector.getContent();
						ArrayList<String> urls = site.setURLsMovies(content);
						for (String string : urls) {
							if (string == null) {
								continue;
							}
							connector.connect(site.URL_SIMPLE + string);
							content = connector.getContent();
							getSubSiteFilmWeb(site, content, connector);
						}

					}

				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
		thread.start();

	}

	public static void getSubSitePunksRock(Parser site, String content) {
		String[] movies = site.getMoviesContent(content);

		for (int i = 1; i < movies.length; i++) {

			System.out.println(site.getTitle(movies[i]));
			System.out.println(site.getYear(movies[i]));
			printArray(site.getGenre(movies[i]));
			printArray(site.getNationality(movies[i]));
			printArray(site.getDirector(movies[i]));
			System.out.println(site.getDuration(movies[i]));
			printArray(site.getCast(movies[i]));
			System.out.println(site.getDescription(movies[i]));
			System.out.println(site.getRating(movies[i]));
		}
	}

	public static void getSubSiteFilmWeb(Parser site, String content,
			Connector connector) throws IOException {
       try{     
        Movie m = new Movie();
        String title = site.getTitle(content);
        if (title == null) {
           title = "";
        }
        //System.out.println(title);
        m.setTitle(title);
        String[] director = new String[] {};
        Integer year = 0;
        year = site.getYear(content);
        if (year == null) {
           year = 1000;
        }
        Premiere premiere = new Premiere();
        premiere.setDateOfPremiere(new Date(year - 1900, 1, 1));
        m.setPremiere(premiere);
        String[] nationality = site.getNationality(content);
        if (nationality != null) {
           ArrayList<Production> productions = new ArrayList<Production>();
           for (String name : nationality) {
              if (name == null) {
                 continue;
              }
              Production production = new Production();
              production.setName(name);
              ArrayList<Movie> movies = new ArrayList<Movie>();
              movies.add(m);
              production.setMovies(movies);
              productions.add(production);
           }
  
           m.setProductions(productions);
        }
        String[] genre = site.getGenre(content);
        if (genre != null) {
           m.setGenres(toGenreList(genre));
        }
        String desc = site.getDescription(content);
        if (desc == null) {
           desc = "";
        }
        m.setDescription(desc);
        Float rating = site.getRating(content);
        if (rating == null) {
           rating = new Float(0);
        }
        m.setRating(rating);
        // m.setPersons(toPersonList(site.getCast(connector.getContent())));
        director = site.getDirector(content);
        connector.connect(connector.getUrl() + "/cast");
        m.setPersons(toPersonList(site.getCast(connector.getContent()),
              director));
        SaveEntityUtil.saveMovie(m);
    }catch(Exception e){
      System.out.println("Coś nie pyklo i polecial wyjatek :(");
    }
	}

	private static List<Person> toPersonList(String[] p_cast,
			String[] p_director) {
		ArrayList<Person> result = new ArrayList<Person>(p_cast.length);
		if (p_cast != null) {
			for (int i = 0; i < p_cast.length; i++) {
				if (p_cast[i] == null) {
					continue;
				}
				Person person = new Person();
				String[] data = p_cast[i].split(" ");
				if (data.length > 1) {
					if (data[0] == null) {
						data[0] = "";
					}
					person.setFirstName(data[0]);
					person.setLastName(arrayToString(
							Arrays.copyOfRange(data, 1, data.length), ""));
				} else {
					person.setLastName(p_cast[i]);
					person.setFirstName(p_cast[i]);
				}
				result.add(person);
			}
		}
		if (p_director != null) {
			for (int i = 0; i < p_director.length; i++) {
				if (p_director[i] == null) {
					continue;
				}
				Person person = new Person();
				String[] data = p_director[i].split(" ");
				if (data.length > 1) {
					if (data[0] == null) {
						data[0] = "";
					}
					person.setFirstName(data[0]);
					person.setLastName(arrayToString(
							Arrays.copyOfRange(data, 1, data.length), ""));
				} else {
					if (p_director[i] == null) {
						p_director[i] = "";
					}
					person.setLastName(p_director[i]);
					person.setFirstName(p_director[i]);
				}
				result.add(person);
			}
		}
		return result;
	}

	private static List<Genre> toGenreList(String[] p_genre) {
		if (p_genre != null) {
			ArrayList<Genre> result = new ArrayList<Genre>(p_genre.length);
			for (int i = 0; i < p_genre.length; i++) {
				if (p_genre[i] == null) {
					continue;
				}
				result.add(new Genre(p_genre[i]));
			}
			return result;
		}
		return new ArrayList<Genre>();

	}

	public static String arrayToString(String[] p_array, String p_pattern) {
		String result = new String();
		if (p_array == null) {
			return "";
		}
		for (String string : p_array) {
			result = result.concat(string + p_pattern);
		}
		return result;

	}

	public static void printArray(String[] p_strings) {
		if (p_strings == null) {
			return;
		}
		for (String string : p_strings) {
			System.out.print(string + " ");
		}
		System.out.println();
	}
}
